package com.quaidi.smartlocker.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quaidi.smartlocker.entity.SysAdmin;
import com.quaidi.smartlocker.mapper.SysAdminMapper;
import com.quaidi.smartlocker.service.SysAdminService;
import com.quaidi.smartlocker.util.PasswordUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 系统管理员Service实现类
 * 
 * @author QuaiDi
 * @since 2025-08-26
 */
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements SysAdminService {

    @Override
    public boolean changePassword(String username, String newPassword) {
        if (username == null || username.trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (newPassword == null || newPassword.trim().isEmpty()) {
            throw new IllegalArgumentException("新密码不能为空");
        }
        
        // 查询管理员
        SysAdmin admin = getByUsername(username);
        if (admin == null) {
            throw new IllegalArgumentException("管理员不存在");
        }
        
        // 生成新的盐值和密码
        String salt = PasswordUtil.generateSalt();
        String encryptedPassword = PasswordUtil.encryptPassword(newPassword, salt);
        
        // 更新密码
        admin.setSalt(salt);
        admin.setPassword(encryptedPassword);
        admin.setUpdateTime(LocalDateTime.now());
        
        return updateById(admin);
    }

    @Override
    public boolean createAdmin(SysAdmin admin) {
        if (admin == null) {
            throw new IllegalArgumentException("管理员信息不能为空");
        }
        if (admin.getUsername() == null || admin.getUsername().trim().isEmpty()) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        if (admin.getPassword() == null || admin.getPassword().trim().isEmpty()) {
            throw new IllegalArgumentException("密码不能为空");
        }
        
        // 检查用户名是否已存在
        SysAdmin existing = getByUsername(admin.getUsername());
        if (existing != null) {
            throw new IllegalArgumentException("用户名已存在");
        }
        
        // 加密密码
        String salt = PasswordUtil.generateSalt();
        String encryptedPassword = PasswordUtil.encryptPassword(admin.getPassword(), salt);
        
        admin.setSalt(salt);
        admin.setPassword(encryptedPassword);
        admin.setStatus(admin.getStatus() == null ? 1 : admin.getStatus());
        admin.setLoginCount(0);
        admin.setCreateTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        
        return save(admin);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        if (id == null) {
            throw new IllegalArgumentException("管理员ID不能为空");
        }
        if (status == null || (status != 0 && status != 1)) {
            throw new IllegalArgumentException("状态值无效，必须为0或1");
        }
        
        SysAdmin admin = getById(id);
        if (admin == null) {
            throw new IllegalArgumentException("管理员不存在");
        }
        
        admin.setStatus(status);
        admin.setUpdateTime(LocalDateTime.now());
        
        return updateById(admin);
    }

    @Override
    public SysAdmin getByUsername(String username) {
        if (username == null || username.trim().isEmpty()) {
            return null;
        }
        
        QueryWrapper<SysAdmin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                   .eq("deleted", 0);
        
        return getOne(queryWrapper);
    }
}